package lib.quasar.pedometer;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.util.Log;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import static android.content.Context.ALARM_SERVICE;

/**
 * description: 0点启动app处理步数
 * created by kalu on 2018/4/3 14:23
 */
public final class PedometerAlertReceive extends BroadcastReceiver {

    public static final String ACTION_STEP_ALERT = "action_step_alert";

    @Override
    public void onReceive(Context context, Intent intent) {
        if (ACTION_STEP_ALERT.equals(intent.getAction())) {
            boolean separate = intent.getBooleanExtra(PedometerService.INTENT_NAME_0_SEPARATE, false);
            Intent stepInent = new Intent(context, PedometerService.class);
            stepInent.putExtra(PedometerService.INTENT_NAME_0_SEPARATE, separate);
            context.startService(stepInent);

            set0SeparateAlertManager(context.getApplicationContext());
            // Log.e("PedometerAlertReceive", "PedometerAlertReceive");
        }
    }

    /**
     * 设置0点分隔Alert，当前天+1天的0点启动
     */
    private final void set0SeparateAlertManager(Context context) {

        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.add(Calendar.DAY_OF_YEAR, 1);
        String tomorrow = dateFormat(calendar.getTimeInMillis(), "yyyy-MM-dd");
        long timeInMillis = getDateMillis(tomorrow + " 00:00:00", "yyyy-MM-dd HH:mm:ss");

        AlarmManager alarmManager = (AlarmManager) context.getSystemService(ALARM_SERVICE);
        Intent i1 = new Intent(context, PedometerAlertReceive.class);
        i1.putExtra(PedometerService.INTENT_NAME_0_SEPARATE, true);
        i1.setAction(PedometerAlertReceive.ACTION_STEP_ALERT);
        PendingIntent operation = PendingIntent.getBroadcast(context, 0, i1, PendingIntent.FLAG_UPDATE_CURRENT);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, timeInMillis, operation);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            alarmManager.setExact(AlarmManager.RTC_WAKEUP, timeInMillis, operation);
        } else {
            alarmManager.set(AlarmManager.RTC_WAKEUP, timeInMillis, operation);
        }

    }

    private final SimpleDateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat();

    private final String dateFormat(long millis, String pattern) {
        SIMPLE_DATE_FORMAT.applyPattern(pattern);
        Date date = new Date(millis);
        String dateString = SIMPLE_DATE_FORMAT.format(date);
        return dateString;
    }

    private final long getDateMillis(String dateString, String pattern) {
        long millionSeconds = 0;
        SIMPLE_DATE_FORMAT.applyPattern(pattern);
        try {
            millionSeconds = SIMPLE_DATE_FORMAT.parse(dateString).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }// 毫秒

        return millionSeconds;
    }
}
